﻿@using hc.epm.UI.Common;
@using hc.epm.DataModel.Basic;
@using hc.Plat.Common.Extend;
@using hc.epm.Common;
@using hc.epm.ViewModel;
@model IEnumerable<UserListView>
@{
    ViewBag.Title = "会员管理";
    Layout = "~/Views/Shared/_LayoutHc.cshtml";
}
<div class="content-right date-wrap fl c">
    <div class="search mt5 ">
        <form class="layui-form" action="" method="get">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <input type="hidden" name="CompanyId" id="CompanyId" value="@ViewBag.CompanyId" />
                    <label class="layui-form-label">用户名 : </label>
                    <div class="layui-input-inline"><input type="text" name="UserName" id="UserName" placeholder="请输入用户名" autocomplete="off" class="layui-input" value="@ViewBag.UserName" /></div>
                    <input type="hidden" id="belong" name="belong" value="@ViewBag.belong" />
                </div>
                <input type="submit" class="layui-btn layui-btn-radius layui-btn-warm" lay-filter="search" value="搜 索" />
            </div>
        </form>
    </div>
    <div class="main mt5">
        <div class="btns">
            <a href="javascript:;" id="add">新增</a>
            <a href="javascript:;" id="delete">删 除</a>
        </div>
        <div class="layui-form">
            <table class="layui-table">
                <thead>
                    <tr>
                        <th width="5%" class="table-center">
                            <input type="checkbox" lay-skin="primary" lay-filter="allChoose" />
                            <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><i class="layui-icon"></i></div>
                        </th>
                        <th width="6%" class="table-center">序号</th>
                        <th width="12%" class="table-left">用户名</th>
                        <th class="table-left">登录账号</th>
                        <th width="12%" class="table-center">联系电话</th>
                        <th width="12%" class="table-center">分配角色</th>
                        <th width="10%" class="table-center">锁定状态</th>
                        <th width="10%" class="table-center">操作</th>
                    </tr>
                </thead>
                <tbody>
                    @{
                        int index = 0;
                    }
                    @if (Model != null && Model.Count() > 0)
                    {
                        foreach (var item in Model)
                        {
                            index++;
                            <tr>
                                <td class="table-center">
                                    @if (item.CompanyType != "Admin")
                                    {
                                        <input type="checkbox" lay-skin="primary" name="chName" />
                                    }
                                    else
                                    {
                                        <input type="checkbox" lay-skin="primary" disabled />
                                    }
                                    <input type="hidden" name="id-@item.UserId" id="id-@item.UserId" value="@item.UserId" />
                                </td>
                                <td class="table-center">@index</td>
                                <td class="table-left">@item.UserName</td>
                                <td class="table-left">@item.RealName</td>
                                <td class="table-center">@item.Phone</td>
                                <td class="table-center ">
                                    <a href="javascript:;" class="set-role text-link " data-itemid="@item.UserId" title="设置角色" data-module="@(item.CompanyType)UserManager" data-right="SetRole" style="color:#1b98b8">设置角色</a>
                                </td>
                                <td class="table-center">
                                    @if (item.IsLock)
                                    {
                                        <a href="javascript:;" class="change-state " data-itemid="@item.UserId" data-value="@item.IsLock" title="账户已锁定" data-module="@(item.CompanyType)UserManager" data-right="unLock"><i class='glyphicon glyphicon-lock handle-red'></i></a>
                                    }
                                    else {
                                        <a href="javascript:;" class="change-state " data-itemid="@item.UserId" data-value="@item.IsLock" title="账户正常" data-module="@(item.CompanyType)UserManager" data-right="unLock"><i class='glyphicon glyphicon-lock handle-blue'></i></a>
                                    }
                                </td>
                                <td class="table-center">
                                    @if (item.CompanyType != "Admin")
                                    {
                                        <a href="javascript:;" class="editor-link " data-itemid="@item.UserId" title="编辑" data-module="@(item.CompanyType)UserManager" data-right="modify"><i class='glyphicon glyphicon-pencil handle-blue'></i></a>
                                    }
                                    <a href="javascript:;" class="detail-link " data-itemid="@item.UserId" title="详情" data-module="@(item.CompanyType)UserManager" data-right="info"><i class='glyphicon glyphicon-list-alt handle-blue'></i></a>
                                </td>
                            </tr>
                        }
                    }
                    else
                    {
                        <tr>
                            <td colspan="10" class="table-center">
                                暂无数据
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            <div id="page"></div>
        </div>
    </div>
</div>
<script type="text/javascript">
    //若无数据隐藏分页组件
    if ('@ViewBag.Total' == 0) {
        $("#page").hide();
    }
    layui.use(['layer', 'form', 'element', 'laypage'], function () {
        var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
        //表格全选
        layui.use('form', function () {
            var $ = layui.jquery, form = layui.form;

            //全选
            form.on('checkbox(allChoose)', function (data) {
                var child = $(data.elem).parents('table').find('tbody input[name="chName"]');
                child.each(function (index, item) {
                    item.checked = data.elem.checked;
                });
                form.render('checkbox');
            });

        });
        laypage.render({
            elem: 'page',
            count: @ViewBag.Total,
            curr: @ViewBag.pageIndex,
            layout: ['prev', 'page', 'next', 'skip', 'count', ],
            jump: function (obj, first) {
                if (!first) {
                    //得到了当前页，用于向服务端请求对应数据
                    var curr = obj.curr;
                    var search = $("form").serialize();
                    window.location.href = "/HCUser/Index?pageIndex=" + curr + "&" + search;
                }
            }
        });
        //删除
        $("#delete").click(function () {
            var chkItems = $('.layui-table tbody input[type="checkbox"]:checked');
            var ids = "";
            chkItems.each(function (index, item) {
                var hfId = $(this).parent().find("input[type='hidden']");
                ids += hfId.val() + ",";
            });
            if (ids != "") {
                layer.confirm("确认要删除所选择的项？", { icon: 3 }, function () {
                    $.post("/HCUser/Delete", { ids: ids, belong: '@ViewBag.Belong' }, function (data) {
                        if (data.Flag) {
                            layer.msg("删除成功", { time: 1000, icon: 1 });
                            location.reload();
                        } else {
                            layer.alert(data.Message, { icon: 2 });
                        }
                    }, "json");
                }, function () {
                    return;
                })

            } else {
                layer.alert("请先选择要删除的数据", { icon: 2 });
            }
        });
    });

    //增加
    $("#add").click(function () {
        layer.open({
            type: 2,
            title: GetLayerTitle("用户管理-新增"),
            shadeClose: false, //点击遮罩关闭层
            area: [BigLayerWidth, '600px'],
            content: '/HCUser/add?Belong=' + '@ViewBag.Belong' + '&CompanyId=' + '@ViewBag.CompanyId' + '&expId=0'
        });
    });
    //分配角色
    $(".set-role").click(function () {
        var id = $(this).data("itemid");
        var belong = $(this).data("module");
        if(belong=="AdminUserManager"){
            belong="Admin";
        }        else{
            belong="Owner";
        }

        layer.open({
            type: 2,
            title: GetLayerTitle("用户角色分配"),
            shadeClose: false, //点击遮罩关闭层
            area: [BigLayerWidth, '600px'],
            content: '/HCUser/setrole?Belong=' + belong + "&Id=" + id
        });
    });
    //锁定状态切换
    $(".change-state").click(function () {
        var id = $(this).data("itemid");
        var value = $(this).data("value");
        var mes = "";
        var state = (value == "True");

        if (id != '') {
            if (value == "False") {
                mes = "确认锁定该用户？"
            } else {
                mes = "确认解锁该用户？"
            }
            layer.confirm(mes, { icon: 3 }, function () {
                $.post("/HCUser/EditState", { id: id,belong:"@ViewBag.belong"}, function (data) {
                    if (data.Flag) {
                        layer.msg("操作成功！", { time: 1000, icon: 1 });
                        location.reload();
                    } else {
                        layer.alert(data.Message, { icon: 2 })
                    }
                })
            })
        }
    })
    //修改
    $(".editor-link").click(function () {
        var id = $(this).data("itemid");
        if (id != "") {
            layer.open({
                type: 2,
                title: GetLayerTitle("用户管理-编辑"),
                shadeClose: false, //点击遮罩关闭层
                area: [BigLayerWidth, '600px'],
                content: '/HCUser/Edit?Belong=' + '@ViewBag.Belong' + "&Id=" + id
            });
        }
    });
    //详情
    $(".detail-link").click(function () {
        var id = $(this).data("itemid");
        if (id != "") {
            layer.open({
                type: 2,
                title: GetLayerTitle("用户管理-详情"),
                shadeClose: false, //点击遮罩关闭层
                area: [BigLayerWidth, '550px'],
                content: '/HCUser/Detail?Belong=' + '@ViewBag.Belong' + "&Id=" + id
            });
        }
    });
</script>
